<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        <meta charset="UTF-8">
        <title>用户管理</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    
    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">演示</a>
                <a>
                    <cite>导航元素</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <div class="layui-card-body ">
                                <form class="layui-form layui-col-space5">

                                    <div class="layui-inline layui-show-xs-block">
                                        <input type="text" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input"></div>
                                    <div class="layui-inline layui-show-xs-block">
                                        <button class="layui-btn" lay-submit="" lay-filter="sreach">
                                            <i class="layui-icon">&#xe615;</i></button>
                                    </div>
                                </form>
                            </div>

                            <!--<table class="layui-table" lay-data="{url:'./user.json',page:true,toolbar: '#toolbarDemo',id:'test'}" lay-filter="test">-->


                            <table id="lm" class="layui-table" lay-data="{url:'/lengmou/iuu/getUsers',
                                    page:true,
                                    toolbar: '#toolbarDemo',
                                    id:'test',
                                    method:'post',
                                    contentType: 'application/json',
                                    request:{pageName: 'pageNum',limitName: 'pageSize'}
                                    }" lay-filter="test">
                                <thead>
                                <tr>
                                    <!--
                                        fileld:表头
                                        title：标题
                                        type:设置类型
                                        edit:'text'编辑文本

                                    -->
                                    <th lay-data="{type:'checkbox'}">ID</th>
                                    <th lay-data="{field:'id', width:80, sort: true}">ID</th>
                                    <th lay-data="{field:'username',  sort: true, edit: 'text'}">用户名</th>
                                    <th lay-data="{field:'password', edit: 'text'}">密码</th>
                                    <th lay-data="{field:'money', sort: true, edit: 'text'}">余额</th>
                                    <th lay-data="{field:'tel',edit: 'text'}">手机号码</th>
                                    <th lay-data="{field:'state', width:100,templet: '#switchTpl'}">状态</th>
                                    <th lay-data="{field:'identity', width: 100,templet:'#switchIdentity'}">身份</th>
                                </thead>
                            </table>
                        </div>
                        <!--<script>-->
                            <!--layui.use('table', function(){-->
                                <!--var table = layui.table;-->
                                <!--table.render({-->
                                    <!--elem:'#lm',-->
                                    <!--url:'/lengmou/iuu/getUsers',-->
                                    <!--page:true,-->
                                    <!--toolbar: '#toolbarDemo',-->
                                    <!--id:'test',-->
                                    <!--method:'post',-->
                                    <!--contentType: 'application/json',-->
                                    <!--request:{pageName: 'pageNum',limitName: 'pageSize'},-->
                                    <!--parseData: function(res){-->
                                        <!--return {-->
                                            <!--"code": '',-->
                                            <!--"msg": 'res.mes',-->
                                            <!--"count": '',-->
                                            <!--"data": 'res.data.item'-->
                                        <!--};-->
                                    <!--}-->
                                <!--});-->

                            <!--});-->
                        <!--</script>-->

                    </div>
                </div>
            </div>
        </div>
    </body>
    <script type="text/html" id="toolbarDemo">
        <div class = "layui-btn-container" > 
            <button class = "layui-btn layui-btn-sm" lay-event = "getCheckData" > 选中封禁 </button>
            <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">选中解封</button >
            <button class = "layui-btn layui-btn-sm" lay-event = "isAll" ><i class="layui-icon"></i>添加用户</button>
        </div > 
    </script>
    <script type="text/html" id="switchTpl">
        <input type = "checkbox" name = "state" username="{{d.username}}" userid="{{d.id}}" value = "{{d.state}}" lay-skin = "switch"lay-text = "正常|封禁" lay-filter = "stateDemo" {{ d.state ==0 ? 'checked': ''}} >
    </script>
    <script type="text/html" id="switchIdentity">
        <input type = "checkbox" name = "identity" username="{{d.username}}" userid="{{d.id}}" value = "{{d.identity}}" lay-skin = "switch"lay-text = "用户|管理" lay-filter = "identityDemo" {{ d.identity ==0 ? 'checked': ''}} >
    </script>
    <script>layui.use('laydate',
        function() {
            var laydate = layui.laydate;

            //执行一个laydate实例
            laydate.render({
                elem: '#start' //指定元素
            });

            //执行一个laydate实例
            laydate.render({
                elem: '#end' //指定元素
            });

        });</script>
    <script>layui.use('table',
        function() {
            var table = layui.table;

            //监听单元格编辑
            table.on('edit(test)',
            function(obj) {
                var value = obj.value, //得到修改后的值
                data = obj.data //得到所在行所有键值
                ,
                field = obj.field; //得到字段
                layer.confirm('您确定修改用户：'+data.username+'吗？', {icon: 3, title:'提示'}, function(index){
                    $.post(
                        "/lengmou/iuu/updataUsers",
                        {
                            id:data.id,
                            username:data.username,
                            password:data.password,
                            identity:data.identity,
                            money:data.money,
                            tel:data.tel
                        },function (date) {
                            if(date.state==1){
                                layer.msg("修改成功");
                            }else if(date.state==-1){
                                location.reload();
                                layer.msg("该用户不存在，或已被删除");
                            }else if(date.state==-2){
                                layer.msg("抱歉，设置金额不能小于0");
                            }else if(date.state==-3){
                                layer.msg("抱歉，该用户名已被使用");
                            }
                        },"json"
                    );
                    layer.close(index);
                },function (index) {
                    location.reload();

                });

            });
            //头工具栏事件
            table.on('toolbar(test)',
            function(obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'getCheckData':
                    var data = checkStatus.data;

                    var test= JSON.stringify(data);
                    layer.confirm('您确定要封禁您选中的这'+data.length+'个用户吗？', {icon: 3, title:'提示'}, function(index) {
                        $.ajax(
                            {
                                type: 'POST',
                                url: '/lengmou/iuu/SttingState1',
                                contentType: "application/json; charset=utf-8",
                                data: test,
                                dataType: 'json',
                                success: function (data) {
                                    if (data.state > 0) {
                                        layer.alert("修改成功",function () {
                                            location.reload();
                                        });
                                    } else if (data.state == -1) {
                                        layer.alert("修改失败，参数错误");
                                    }
                                }
                            }
                        );
                        layer.close(index);

                    },function (index) {
                        layer.close(index);
                    });

                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    var test= JSON.stringify(data);
                    layer.confirm('您确定要解封您选中的这'+data.length+'个用户吗？', {icon: 3, title:'提示'}, function(index) {
                        $.ajax(
                            {
                                type: 'POST',
                                url: '/lengmou/iuu/SttingState0',
                                contentType: "application/json; charset=utf-8",
                                data: test,
                                dataType: 'json',
                                success: function (data) {
                                    if (data.state > 0) {
                                        layer.alert("修改成功",function () {
                                            location.reload();
                                        });
                                    } else if (data.state == -1) {
                                        layer.alert("修改失败，参数错误");
                                    }
                                }
                            }
                        );
                        layer.close(index);
                    },function (index) {
                        layer.close(index);
                    });
                    break;
                case 'isAll':
                    // layer.msg(checkStatus.isAll ? '全选': '未全选');
                    xadmin.open('添加用户','./member-add.html',600,400);
                    break;
                };
            });
        });

    layui.use('form', function(){
        var form = layui.form;
        form.on('submit(sreach)', function(data){
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            layui.use('table',
                function() {
                    var table = layui.table;
                    table.reload('test', {
                        url: '/lengmou/iuu/searchUsersName',
                        where: { //设定异步数据接口的额外参数，任意设
                            username: data.field.username
                        }
                        ,page: false,
                        toolbar: '#toolbarDemo'
                        // id: 'test'
                    });
                });
             //只重载数据
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        form.on('switch(stateDemo)', function(data){
            var checked = data.elem.checked;
            var userid=data.elem.attributes['userid'].nodeValue;
            var username=data.elem.attributes['username'].nodeValue;
            if(!data.elem.checked&&data.value==0){
                layer.confirm('您确定要封禁用户：'+username+'吗？', {icon: 3, title:'提示'}, function(index){
                        $.post(
                            "/lengmou/iuu/setUsersState",
                            {id:userid},
                            function (data) {
                                if(data.state==1){
                                    layer.msg("修改成功");
                                    location.reload();
                                }else if(data.state==-1){
                                    layer.msg("该用户不存在，或已被删除");
                                    location.reload();
                                }else{
                                    layer.msg("抱歉，服务器故障");
                                }
                            },"json"
                        );
                },function (index,obj) {
                    layer.msg("选择取消");
                    data.elem.checked=!checked;
                    form.render();
                });
            }else if(data.elem.checked){
                layer.confirm('您确定要解封用户：'+username+'吗？', {icon: 3, title:'提示'}, function(index){
                    $.post(
                        "/lengmou/iuu/setUsersState",
                        {id:userid},
                        function (data) {
                            if(data.state==1){
                                layer.msg("修改成功");
                                location.reload();
                            }else if(data.state==-1){
                                layer.msg("该用户不存在，或已被删除");
                                location.reload();
                            }else{
                                layer.msg("抱歉，服务器故障");
                            }
                        },"json"
                    );
                },function (index,obj) {
                    layer.msg("选择取消");
                    data.elem.checked=!checked;
                    form.render();
                });
            }
        });
        form.on('switch(identityDemo)', function(data){
            var checked = data.elem.checked;
            var userid=data.elem.attributes['userid'].nodeValue;
            var username=data.elem.attributes['username'].nodeValue;
            if(!data.elem.checked&&data.value==0){
                layer.confirm('您确定要设置用户：'+username+'为管理员吗？', {icon: 3, title:'提示'}, function(index){
                    $.post(
                        "/lengmou/iuu/setUsersIdentity",
                        {id:userid},
                        function (data) {
                            if(data.state==1){
                                layer.msg("修改成功");
                                location.reload();
                            }else if(data.state==-1){
                                layer.msg("该用户不存在，或已被删除");
                                location.reload();
                            }else{
                                layer.msg("抱歉，服务器故障");
                            }
                        },"json"
                    );
                },function (index,obj) {
                    layer.msg("选择取消");
                    data.elem.checked=!checked;
                    form.render();
                });
            }else if(data.elem.checked){
                layer.confirm('您确定要取消用户：'+username+'的管理权限吗？', {icon: 3, title:'提示'}, function(index){
                    $.post(
                        "/lengmou/iuu/setUsersIdentity",
                        {id:userid},
                        function (data) {
                            if(data.state==1){
                                layer.msg("修改成功");
                                location.reload();
                            }else if(data.state==-1){
                                layer.msg("该用户不存在，或已被删除");
                                location.reload();
                            }else{
                                layer.msg("抱歉，服务器故障");
                            }
                        },"json"
                    );
                },function (index,obj) {
                    layer.msg("选择取消");
                    data.elem.checked=!checked;
                    form.render();
                });
            }
        });
    });

    </script>
    <script>var _hmt = _hmt || []; (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();</script>

</html>